<?php
class AlertController extends BaseController{
    public function init(){
        
    }
    

    public function indexAction(){
        
        $request =$this->getRequest();
         
        $mMotor = new Motor ();
        if ($this->type){
            $this->view->HTML_FACTORY_1 = $this->writeoption ($this->view->ARR_FACTORY);
            $list = $mMotor->getManufacture(0,'name','ASC');            
            $this->view->HTML_FACTORY_0 = $this->writeoption ($list);
            
            $this->view->HTML_MODEL_1 = $this->writeoption ($this->view->ARR_MODEL);
            $list = $mMotor->getModel(0,'name','ASC');            
            $this->view->HTML_MODEL_0 = $this->writeoption ($list);
            
            $this->view->HTML_PRICE_1 = $this->writeoption ($this->view->ARR_PRICE,'alias','display');
            $list = $mMotor->getPrice(0);            
            $this->view->HTML_PRICE_0 = $this->writeoption ($list,'alias','display');
            
        }else{
            $this->view->HTML_FACTORY_0 = $this->writeoption ($this->view->ARR_FACTORY);       
            $list = $mMotor->getManufacture(1,'name','ASC');
            $this->view->HTML_FACTORY_1 = $this->writeoption ($list);
            
            $this->view->HTML_MODEL_0 = $this->writeoption ($this->view->ARR_MODEL);
            $list = $mMotor->getModel(1,'name','ASC');
            $this->view->HTML_MODEL_1 = $this->writeoption ($list);
            
            $this->view->HTML_PRICE_0 = $this->writeoption ($this->view->ARR_PRICE,'alias','display');
            $list = $mMotor->getPrice(1);            
            $this->view->HTML_PRICE_1 = $this->writeoption ($list,'alias','display');
        }
        
        $hidden_login = $request->getPost('hidden_login','');
        if($request->isPost() && $hidden_login==''){
            $post = $request->getPost();
            
            $post['birthdate'] = str_replace ('/','-', $post['birthdate']);
            
            $rules = array (
                //'first_name'=>'req',
                'last_name'=>'req',                
                'birthdate'=> array('date'=>'d-m-Y'),
                //'address'=>'req',
                'city'=>'req',                
                'phone'=>'req',
                'agree'=>'req'
            );
            $arrAlert=array(
                //'first_name'=>'Tên',
                'last_name'=>'Họ và tên',                
                'birthdate'=> 'Sai định dạng ngày tháng',
                //'address'=>'Địa chỉ bắt buộc phải nhập',
                'city_id'=>'Bạn chưa chọn thành phố',                
                'phone'=>'Số điện thoại chưa nhập',
                'agree'=>'Bạn phải chấp nhận những điều khoản của website',
                'captcha'=>'Sai mã xác nhận',
                'alert'=>'Bạn phải chọn ít nhất 1 tiêu chí tìm kiếm'
            );
            if (!isset($post['agree']) || $post['agree']==0){
                $post['agree'] = '';
            }
            
            $oValidation = new Defined_Validation($rules);
            //var_dump($oValidation->rules);die;
            
            $valid = $oValidation->validate($post);
            
            if ($valid===true){
                unset ($post['agree']);
                list ($d,$m, $y) = explode('-', $post['birthdate']);
                $post['birthdate'] = "$y-$m-$d";
                $data = $data1 =$data2 = array ();
                $error = 1;
                $valid = 'alert';
                if( $post['alert1']['manu_id']!=''
                    || $post['alert1']['model_id']!=''
                    || $post['alert1']['city_id']!=''
                    || $post['alert1']['price']!=''
                    ){
                    //$post['alert1'] = implode ('/', $post['alert1']);
                    $data = $post;
                    unset ($data['alert1'], $data['alert2'], $data['alert3']);
                    $data = array_merge ($data, $post['alert1']);
                    
                    if ($data['price']){
                        list ($data['price_from'],$data['price_to']) = explode ('-', $data['price']);
                        
                    }else{
                        $data['price_from'] = $data['price_to'] = '';
                    }
                    unset ($data['price'],$data['captcha_code']);
                    
                    $error =0;
                    
                }
                if(
                    $post['alert2']['manu_id']!=''
                    || $post['alert2']['model_id']!=''
                    || $post['alert2']['city_id']!=''
                    || $post['alert2']['price']!=''
                    ){
                    
                    //$post['alert2'] = implode ('/', $post['alert2']);
                    $data1 = $post;
                    unset ($data1['alert1'], $data1['alert2'], $data1['alert3']);
                    
                    $data1 = array_merge ($data1, $post['alert2']);
                    
                    if ($data1['price']){
                        list ($data1['price_from'],$data1['price_to']) = explode ('-', $data1['price']);
                        
                    }else{
                        $data1['price_from'] = $data1['price_to'] = '';
                    }
                    unset ($data1['price'],$data1['captcha_code']);  
                    
                    $error =0;
                    
                }
                
                if ($post['captcha_code'] != $_SESSION['security_code']){
                    $error =1;
                    $valid = 'captcha';
                    unset ($_SESSION['security_code']);
                }
                /*if(
                    $post['alert3']['manu_id']!=''
                    || $post['alert3']['model_id']!=''
                    || $post['alert3']['city_id']!=''
                    || $post['alert3']['price']!=''
                    ){
                    //$post['alert3'] = implode ('/', $post['alert3']);
                    $data2 = $post;
                    unset ($data2['alert1'], $data2['alert2'], $data2['alert3']);
                    
                    $data2 = array_merge ($data2, $post['alert3']);
                    
                    if ($data2['price']){
                        list ($data2['price_from'],$data2['price_to']) = explode ('-', $data2['price']);
                        
                    }else{
                        $data2['price_from'] = $data2['price_to'] = '';
                    }
                    unset ($data2['price']);
                    
                    $error =0;
                    
                }*/
                
               
                if($error){
                    $this->view->CALL_SCRIPT = 'alert ("'.$arrAlert[$valid].'");' . "\n";
                }else{                        
                    
                    $mAlert = new Alert();
                              
                    $id = $mAlert->insert($data , $data1 );
                    if($id>0){
                        $this->view->CALL_SCRIPT = 'alert ("Bạn đã đăng ký nhận email thông báo thành công.");' . "\n";
                    }else{
                        $this->view->CALL_SCRIPT = 'alert ("Chỉ được phép đặt 2 email thông báo.");' . "\n";
                    }
                }
                
                   
            }else{
                $this->view->CALL_SCRIPT = 'alert ("'.$arrAlert[$valid].'");' . "\n";
            }
            //var_dump($post);
        }
        
        //$this->view->CHECKFORM = Common::check_input('last_name,first_name,email,age,phone,address,city_id','alert');// Check input
        $authenticate = new Defined_Authenticate();    
        if($authenticate->isLogged()===true){      
            
            $this->view->LNAME = $this->auth->last_name;
            //$this->view->FNAME = $this->auth->first_name;
            $this->view->EMAIL = $this->auth->email;
            $this->view->PHONE = $this->auth->phone;
            //$this->view->ADDRESS = $this->auth->address;
            $this->view->CITY = $this->auth->city_id;
            
            $this->view->BIRTHDATE = date('d/m/Y', strtotime($this->auth->birthdate));
        }else{
            $this->view->LNAME = $post['last_name'];
            //$this->view->FNAME = $this->auth->first_name;
            $this->view->EMAIL = $post['email'];
            $this->view->PHONE = $post['phone'];
            //$this->view->ADDRESS = $this->auth->address;
            $this->view->CITY = $post['city'];
            
            $this->view->BIRTHDATE = date('d/m/Y', strtotime($post['birthdate']));
        }
        unset ($mAlert);
    }   
    private function writeoption ($arrdata, $field_value='id', $field_name='name'){
        $html ='';
        foreach ($arrdata as $row){
            $html .= '<option value="'.$row[$field_value].'">'.$row[$field_name].'</option>';
        }
        return $html;
    } 
}
?>